//
//  YYLComposeToolBar.swift
//  XinlangWeibo
//
//  Created by yangyinglei on 2017/4/6.
//  Copyright © 2017年 yangyinglei. All rights reserved.
//

import UIKit

enum composeToolBarBtnType: Int {
    case camera = 0
    case mention
    case trend
    case emoticon
    case add
}

class YYLComposeToolBar: UIStackView {

    //设置回调的闭包
    var callBack: ((UIButton)->())?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    private func setupUI() {
    //这个容器没有渲染能力 不能设置背景色
    //相机 @ # 笑脸  +
        addChildBtn(imageName: "compose_camerabutton_background", btnType: .camera)
        addChildBtn(imageName: "compose_mentionbutton_background", btnType: .mention)
        addChildBtn(imageName: "compose_trendbutton_background", btnType: .trend)
        addChildBtn(imageName: "compose_emoticonbutton_background", btnType: .emoticon)
        addChildBtn(imageName: "compose_add_background", btnType: .add)

        axis = .horizontal
        distribution = .fillEqually
        
    }
    private func addChildBtn(imageName: String, btnType: composeToolBarBtnType) {
        let btn = UIButton()
        btn.tag = btnType.rawValue
        //添加点击事件
        btn.addTarget(self, action: #selector(boolBarBtnAction(btn:)), for: .touchUpInside)
        btn.setImage(UIImage(named: imageName), for: .normal)
        btn.setImage(UIImage(named: imageName + "_highlighted"), for: .highlighted)
        btn.setBackgroundImage(UIImage(named: "compose_toolbar_background"), for: .normal)
        btn.sizeToFit()
        //UIStackView 子按钮应该放在这个容器上  不能直接添加在view上
        btn.adjustsImageWhenHighlighted = false
        addArrangedSubview(btn)
    }
    
    @objc private func boolBarBtnAction(btn: UIButton) {
        callBack?(btn)
    
    }
}


